package com.sun.springboot_mq1.sender;

import com.rabbitmq.client.MessageProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**消息生产者代码
 * @author shkstart
 * @create 2021-10-24 22:44
 */
@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {
    public static final String Delayed_QUEUE="delayed.queue";
    public static final String Delayed_EXCHANGE="delayed.exchange";
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info(" 当前时间：{}, 发送一条信息给两个 TTL 队列:{}", new Date(), message);
        rabbitTemplate.convertAndSend("X","XA"," 消息来自 ttl 为 为 10S 的队列: "+message);
        rabbitTemplate.convertAndSend("X","XB"," 消息来自 ttl 为 为 40S 的队列: "+message);
    }
    @GetMapping("sendExpirationMsg/{message}/{ttlTime}")
    public void sendMsg(@PathVariable String message,@PathVariable String ttlTime) {
        rabbitTemplate.convertAndSend("X", "XC", message, correlationData ->{
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });
        log.info(" 当前时间：{}, 发送一条时长{} 毫秒 TTL 信息给队列 C:{}", new Date(),ttlTime, message);
    }
    @GetMapping("sendDelayMsg/{message}/{delayedTime}")
    public void sendMsg(@PathVariable String message,@PathVariable Integer delayedTime){
        this.rabbitTemplate.convertAndSend(Delayed_EXCHANGE,"delayed.routingKey",message,correlationData->{
            correlationData.getMessageProperties().setDelay(delayedTime);
            return correlationData;
        });
        log.info(" 当 前 时 间 ： {}, 发 送 一 条 延 迟 {} 毫 秒 的 信 息 给 队 列 delayed.queue:{}",new Date(),delayedTime,message);
    }
}
